package com.example.word_book.utils;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.word_book.R;
import com.example.word_book.bean.ErrorWord;
import com.example.word_book.bean.Word;
import com.example.word_book.service.BrowseWordActivity;

public class FragmentHelp extends Fragment {
    private Word word;
    private View view;
    private LayoutInflater mLayoutInflater;

    public FragmentHelp(Word word) {
        this.word = word;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_word_shorthand, container, false);
        if (view != null) {
            init();
        }

        mLayoutInflater = getLayoutInflater();

        return view;
    }

    private void init() {
        TextView tv_word = view.findViewById(R.id.tv_word);
        Button btn_remember = view.findViewById(R.id.btn_remember);
        Button btn_vague = view.findViewById(R.id.btn_vague);
        ListView lv = view.findViewById(R.id.lv_translation);

        tv_word.setText(word.getWord());

        btn_remember.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lv.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return word.getType().length;
                    }

                    @Override
                    public Object getItem(int i) {
                        return word.getType()[i];
                    }

                    @Override
                    public long getItemId(int i) {
                        return i;
                    }

                    @Override
                    public View getView(int i, View view, ViewGroup viewGroup) {
                        view = mLayoutInflater.inflate( R.layout.list_item_translation, null);
                        TextView tv_type = view.findViewById(R.id.tv_type);
                        TextView tv_translation = view.findViewById(R.id.tv_translation);
                        tv_type.setText(word.getType()[i]);
                        tv_translation.setText(String.valueOf(word.getTranslation()[i]));
                        return view;
                    }
                });
                new SqlHelp(getContext()).deleteErrorWord(word.getWord());
            }
        });
        btn_vague.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lv.setAdapter(new BaseAdapter() {
                    @Override
                    public int getCount() {
                        return word.getType().length;
                    }

                    @Override
                    public Object getItem(int i) {
                        return word.getType()[i];
                    }

                    @Override
                    public long getItemId(int i) {
                        return i;
                    }

                    @Override
                    public View getView(int i, View view, ViewGroup viewGroup) {
                        view = mLayoutInflater.inflate( R.layout.list_item_translation, null);
                        TextView tv_type = view.findViewById(R.id.tv_type);
                        TextView tv_translation = view.findViewById(R.id.tv_translation);
                        tv_type.setText(word.getType()[i]);
                        tv_translation.setText(String.valueOf(word.getTranslation()[i]));
                        return view;
                    }

                });
                SqlHelp sqlHelp = new SqlHelp(getContext());
                sqlHelp.insertErrorWord(new ErrorWord(word.getWord()));
            }
        });
    }
}
